<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Controller;
use think\Request;
use think\Db;
use think\db\Query;

class SubjectController extends AdminBaseController
{
    //显示
    public function index()
    {
        //搜索条件
        $name = $this->request->param('name');
		//接收teacher_id的值
        $teacher_id = $this->request->param('teacher_id');
		//查询学校列表的数据         
        $teacher = Db::name('teacher') ->select();
        //连接数据库可以去掉表前缀，此方法可自己读取数据库配置文件中的表前缀
        $subject = Db::name('subject')
            //表连接
            ->join('cmf_teacher','cmf_subject.teacher_id=cmf_teacher.id','LEFT')
            //查询数据
            ->field('cmf_subject.*')
            ->field('cmf_teacher.name as teacher_name')
            //接收所查询的值,使用where的闭包函数查询
            ->where(function(Query $query) use ($name,$teacher_id){
				//学校名称搜索
                if ($teacher_id) {
                    $query->where('cmf_teacher.id', 'eq', $teacher_id );
                }
                //班级名称搜索
                if($name){
                    $query->where('cmf_subject.name','like',"%$name%");
                }
            })   
            //倒序排列（ASC为正序）       
            ->order("id DESC")
            //分页：每页条数，跳转路径，额外参数
            ->paginate(3, false, ['query' => request()->param()]);
        //渲染分页
        $page = $subject->render();
        //渲染页面，传参
        return view('index',[
            'subject' => $subject,
            'page' => $page,
            'teacher' => $teacher,
            'teacher_id' => $teacher_id]);
    }

    //添加
    public function add()
    {
        //判断提交类型
        if (request()->isPost()){
            //获取当前id提交的全部数据
            $data = input();
            //判断班级名称是否为空
            $where = ['name' => $data['name']];
            //查询数据库，判断是否存在相同数据
            $hassubject = Db::name('subject')->where($where)->find();
            if ($hassubject){
                $this->error('班级名称已存在');
            }
            //添加数据
            $add = Db::name('subject')->insert($data);
            //判断是否添加成功
            if ($add){
                $this->success('添加成功','subject/index');
            }else{
                $this->error('添加失败');
            }
        }
        //查询学校信息
        $subject = Db::name('teacher') -> select();
        //渲染页面，传参
        return view('subject_add',['subject' => $subject]);
    }

    //编辑
    public function edit()
    {
        //判断提交类型，是否为post提交
        if (request()->isPost()){
            //获取当前id对应的全部数据
            $data = input();
            //获取数据库中的id，修改数据
            if (empty($data['name'])){
                $this->error('班级名称不能为空');
            }
            //判断是否修改成功
            $edit = Db::name('subject')->update($data,['id'=>$data['id']]);
            if ($edit){
                $this->success('修改成功','subject/index');
            }else{
                $this->error('修改失败');
            }
        }
        //获取当前id
        $id = input('id');
        //查询要编辑的记录
        $subject = Db::name('subject')->get($id);
        //获取学校列表中的所有信息
        $teacher = Db::name('teacher')->select();
        //渲染页面，传参
        return view('subject_edit',['subject'=>$subject,'teacher'=>$teacher]);
    }

    //删除
    public function delete()
    {
        //获取当前id对应的全部数据
        $data = input();
        //获取数据库中的id，删除数据
        $del = Db::name('subject')->delete($data['id']);
        //判断是否删除成功
        if($del){
            $this->success('删除成功','subject/index');
        }else{
            $this->error('删除失败');
        }
    }

    //多选删除
    public function delAll()
    {
        $ids = input('ids/a');
        $del =  Db::name('subject')->delete($ids);
        if ($del){
            return ['code'=>200,'msg'=>'成功'];
        }else{
            return ['code'=>100,'msg'=>'失败'];
        }
    }
}